﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Basic_readmsg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["tid"] == null)
                Response.Redirect("../Error/Error.htm");
            if (Request.QueryString["code"] != null || Request.QueryString["type"]!=null)
            {
                string id = Request.QueryString["code"].ToString().Trim();
                string flag = Request.QueryString["type"].ToString().Trim();
                this.HiddenField1.Value = id;
                PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
                setModel(Mail.GetMessage(id),flag);
                //标记已读
                Mail.TagRead(id);
            }
        }
    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        int result = 0;
        PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
        if (this.labTag.Text == "消息来自")
            result = Mail.Delete_Msg(this.HiddenField1.Value, "instatus=1");
        else
            result = Mail.Delete_Msg(this.HiddenField1.Value, "outstatus=1");
        if (result > 0)
            Response.Redirect("maildustbin.aspx");
    }
    protected void setModel(SqlDataReader myreader,string type)
    {
        if (myreader.HasRows)
        {
            if (type == "receive")
            {
                string toid = "";
                this.labTag.Text = "消息来自";
                if (myreader.Read())
                {
                    toid = myreader[0].ToString();
                    this.labFrom.Text = myreader[1].ToString();
                    this.labTitle.Text = myreader[4].ToString();
                    this.labContent.Text = myreader[5].ToString();
                    this.labTime.Text = myreader[6].ToString();
                    this.labType.Text=myreader[7].ToString();
                }
                myreader.Close();
                this.hlReply.Visible = true;
                this.hlReply.NavigateUrl = "mailsend.aspx?to="+this.labFrom.Text+"&id="+toid.Trim();
            }
            else if (type == "send")
            {
                this.labTag.Text = "消息发给";
                if (myreader.Read())
                {
                    this.labFrom.Text = myreader[3].ToString();
                    this.labTitle.Text = myreader[4].ToString();
                    this.labContent.Text = myreader[5].ToString();
                    this.labTime.Text = myreader[6].ToString();
                    this.labType.Text = myreader[7].ToString();
                }
                myreader.Close();
                this.hlReply.Visible = false;
            }
            else
                Response.Redirect("../Error/Error.htm");
            string typename=this.labType.Text.Trim();
            if (typename == "1")
                this.labType.Text = "普通邮件";
            else if (typename == "2")
                this.labType.Text = "学院邮件";
            else if (typename == "3")
                this.labType.Text = "项目邮件";
            else
                this.labType.Text = "系统邮件";

        }
        else
            Response.Redirect("../Error/Error.htm");
    }
}
